package com.ruoyi.dl.service.impl;

import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.dl.domain.DlOrders;
import com.ruoyi.dl.domain.DlOrdersGoods;
import com.ruoyi.dl.domain.DlUser;
import com.ruoyi.dl.mapper.DlOrdersGoodsMapper;
import com.ruoyi.dl.mapper.DlOrdersMapper;
import com.ruoyi.dl.mapper.DlUserMapper;
import com.ruoyi.dl.service.IDlOrdersGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 发单商品Service业务层处理
 *
 * @author ruoyi
 * @date 2020-12-28
 */
@Service
public class DlOrdersGoodsServiceImpl implements IDlOrdersGoodsService {
    @Autowired
    private DlOrdersGoodsMapper dlOrdersGoodsMapper;

    @Autowired
    private DlUserMapper dlUserMapper;

    @Autowired
    private DlOrdersMapper ordersMapper;

    /**
     * 查询发单商品
     *
     * @param id 发单商品ID
     * @return 发单商品
     */
    @Override
    public DlOrdersGoods selectDlOrdersGoodsById(Integer id) {
        return dlOrdersGoodsMapper.selectDlOrdersGoodsById(id);
    }

    /**
     * 新建查询发单商品
     *
     * @param id 发单商品ID
     * @return 发单商品
     */
    @Override
    public DlOrdersGoods newSelectDlOrdersGoodsById(Integer id) {
        return dlOrdersGoodsMapper.newSelectDlOrdersGoodsById(id);
    }

    /**
     * 根据用户id查询个人我接受的商单列表
     *
     * @param userId     用户id
     * @param ordersType 订单状态
     * @return
     */
    @Override
    public List<DlOrdersGoods> selectDlOrdersGoodsByUserId(Integer userId, Integer ordersType, Integer pageIndex, Integer pageSize) {
        return dlOrdersGoodsMapper.selectDlOrdersGoodsByUserId(userId, ordersType, pageIndex, pageSize);
    }

    /**
     * 根据用户id查询个人我发布的的商单列表
     *
     * @param userId     发布人id
     * @param ordersType 订单状态
     * @param pageIndex  索引
     * @param pageSize   条数
     * @return
     */
    @Override
    public List<DlOrdersGoods> selectDlOrdersGoodsByFBUserId(Integer userId, Integer ordersType, Integer pageIndex, Integer pageSize) {
        return dlOrdersGoodsMapper.selectDlOrdersGoodsByFBUserId(userId, ordersType, pageIndex, pageSize);
    }

    /**
     * 查询发单商品列表
     *
     * @param dlOrdersGoods 发单商品
     * @return 发单商品
     */
    @Override
    public List<DlOrdersGoods> selectDlOrdersGoodsList(DlOrdersGoods dlOrdersGoods) {
        return dlOrdersGoodsMapper.selectDlOrdersGoodsList(dlOrdersGoods);
    }

    /**
     * 查询发单商品列表
     *
     * @param dlOrdersGoods 发单商品
     * @return 发单商品
     */
    @Override
    public List<DlOrdersGoods> newSelectDlOrdersGoodsList(DlOrdersGoods dlOrdersGoods) {
        List<DlOrdersGoods> ordersGoods = dlOrdersGoodsMapper.newSelectDlOrdersGoodsList(dlOrdersGoods);
        for (DlOrdersGoods ordersGood : ordersGoods) {
            DlOrders dlOrders = new DlOrders();
            dlOrders.setOrdersGoodsId(ordersGood.getId());
            List<DlOrders> orders = ordersMapper.selectDlOrdersList(dlOrders);
            if (orders.size() > 0 && orders != null) {
                ordersGood.getOrders().setOrdersType(orders.get(0).getOrdersType());
                DlUser dlUser = dlUserMapper.selectDlUserById(orders.get(0).getUserId());
                if (dlUser != null) {
                    ordersGood.setTaker(dlUser.getWxName());
                }
            }
        }
        return ordersGoods;
    }

    /**
     * 查询所有未接商单列表排序
     *
     * @param dlOrdersGoods 商单信息
     * @return 结果
     */
    @Override
    public List<DlOrdersGoods> newSelectSearchDlOrdersGoodsALLSortService(DlOrdersGoods dlOrdersGoods) {
        return dlOrdersGoodsMapper.newSelectSearchDlOrdersGoodsALLSortService(dlOrdersGoods);
    }

    /**
     * 查询所有未接商单列表
     *
     * @param dlOrdersGoods 信息
     * @return 结果
     */
    @Override
    public List<DlOrdersGoods> newSelectSearchDlOrdersGoodsNotInOrders(DlOrdersGoods dlOrdersGoods) {
        return dlOrdersGoodsMapper.newSelectSearchDlOrdersGoodsNotInOrders(dlOrdersGoods);
    }

    /**
     * 查询所有已接商单列表
     *
     * @param dlOrdersGoods 信息
     * @return 结果
     */
    @Override
    public List<DlOrdersGoods> newSelectSearchDlOrdersGoodsInOrders(DlOrdersGoods dlOrdersGoods) {
        return dlOrdersGoodsMapper.newSelectSearchDlOrdersGoodsInOrders(dlOrdersGoods);
    }

    /**
     * 新增发单商品
     *
     * @param dlOrdersGoods 发单商品
     * @return 结果
     */
    @Override
    public int insertDlOrdersGoods(DlOrdersGoods dlOrdersGoods) {
        dlOrdersGoods.setCreateTime(DateUtils.getNowDate());
        return dlOrdersGoodsMapper.insertDlOrdersGoods(dlOrdersGoods);
    }

    /**
     * 修改发单商品
     *
     * @param dlOrdersGoods 发单商品
     * @return 结果
     */
    @Override
    public int updateDlOrdersGoods(DlOrdersGoods dlOrdersGoods) {
        return dlOrdersGoodsMapper.updateDlOrdersGoods(dlOrdersGoods);
    }

    /**
     * 删除发单商品对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteDlOrdersGoodsByIds(String ids) {
        return dlOrdersGoodsMapper.deleteDlOrdersGoodsByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除发单商品信息
     *
     * @param id 发单商品ID
     * @return 结果
     */
    @Override
    public int deleteDlOrdersGoodsById(Integer id) {
        return dlOrdersGoodsMapper.deleteDlOrdersGoodsById(id);
    }
}
